package xyz.jiang.asteroid.common.base;

import lombok.Data;
import xyz.jiang.asteroid.common.constants.RestResponseStatus;
import xyz.jiang.asteroid.common.enums.ServiceExceptionEnum;

@Data
public class RestResponse<T> {

    private Integer code;

    private String status;

    private String msg;

    private T data;

    private static final int OK_CODE = 200;
    private static final String OK_STATUS = RestResponseStatus.SUCCESS;

    public RestResponse(Integer code,String status, String msg, T data) {
        this.code = code;
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public RestResponse(Integer code, String status,String msg) {
        this.code = code;
        this.status = status;
        this.msg = msg;
    }
    public RestResponse(Integer code, String status) {
        this.code = code;
        this.status = status;
    }

    public RestResponse() {
        this(OK_CODE, OK_STATUS);
    }

    public static <T> RestResponse<T> success(){
        return new RestResponse<>();
    }

    public static <T> RestResponse<T> success(String msg){
        return new RestResponse<>(OK_CODE,OK_STATUS,msg);
    }

    public static <T> RestResponse<T> success(T data) {
        RestResponse<T> response = new RestResponse<>();
        response.setData(data);
        return response;
    }

    public static <T> RestResponse error(Integer code, String msg) {
        return new RestResponse(code, RestResponseStatus.ERROR,msg);
    }

    public static <T> RestResponse error(ServiceExceptionEnum exceptionEnum) {
        return new RestResponse(exceptionEnum.getCode(),RestResponseStatus.ERROR, exceptionEnum.getMsg());
    }

}
